package com.doget.data.dialog;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.doget.comment.utils.DisplayUtil;
import com.doget.data.R;

public class MyDialog extends Dialog {

    private Window window = null;

    public MyDialog(Context context) {
        super(context, R.style.CustomProgressDialog);
    }

    public void showDialog(final Context context) {
        @SuppressLint("InflateParams")
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_out_am, null);

        ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        lp.width = (int) (DisplayUtil.getScreenWidth(context));


        final LinearLayout linearLayout = view.findViewById(R.id.linear);
        final TextView textView1 = view.findViewById(R.id.textView1);
        textView1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                windowDeploy(linearLayout ,context);
            }
        });

        //设置触摸对话框意外的地方取消对话框
        setCanceledOnTouchOutside(true);
        addContentView(view, lp);
        show();
        Window window =  getWindow();
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        layoutParams.gravity = Gravity.CENTER; // 居中位置
        layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(layoutParams);
        window.setWindowAnimations(R.style.dialogWindowAnim);  //添加动画
    }

    //设置窗口显示
    public void windowDeploy(View view, Context context) {
//        动画文件可以存放在res下的anim目录 Android Studio的话 需要自己手动创建这个文件夹
//        比如说我在anim下创建了一个动画文件名为scaleanim.xml那么在Java中引用的话
//加载动画对象
        Animation animation = AnimationUtils.loadAnimation(context, R.anim.dialog_enter_anim_a);
//启动动画
        view.startAnimation(animation);
//另外 动画animation还有一个执行监听事件  使用如下 使用场景比如说启动页面 动画结束后跳转页面

        animation.setAnimationListener(new Animation.AnimationListener() {
            /**
             * 动画启动
             * @param animation
             */
            @Override
            public void onAnimationStart(Animation animation) {

            }

            /**
             * 动画结束
             * @param animation
             */
            @Override
            public void onAnimationEnd(Animation animation) {

            }

            /**
             * 动画重复
             * @param animation
             */
            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }
}
